/* STAT DES TABLE 1 */
/* IMMIGRANT SHARE IN POPULATION */
/* SHARE RECENT ARRIVALS IN POPULATION  */
/* DISTRIBUTION RECENT ARRIVALS BY GEOGRAPHICAL ORIGINS */
/************/
/* Original */
libname tbrp "C:\Users\Public\Documents\donnees\TbRP\mini";
libname tbrpmin "C:\Users\Public\Documents\donnees\TbRP\mini";
libname result   'C:\Users\Public\Documents\Greg_JEG\ShiftShare';
libname table "C:\Users\Public\Documents\Greg_JEG\Tablesmodif";
libname ccom "C:\Users\Public\Documents\donnees\Code_commune";

libname harmo "C:\Users\Public\Documents\donnees\RP_Harmo\"; /* taille longue */
libname hmini "C:\Users\Public\Documents\donnees\RP_Harmo\mini"; /* petite taille */

options ls=80 mprint ;

/* Table 1 Panel A & B */
title '1982';
data rp;
set harmo.hrp82 (keep = immig nimmig sond pnaic);
run;
proc freq data=rp;
table immig nimmig nimmig*pnaic;
run;
title '1990';
data rp;
set harmo.hrp90 (keep = immig nimmig sond pnaic);
run;
proc freq data=rp;
table immig nimmig nimmig*pnaic;
run;
title '1999';
data rp;
set harmo.hrp99 (keep = immig nimmig sond pnaic);
run;
proc freq data=rp;
table immig nimmig nimmig*pnaic;
run;
title '2007';
data rp;
set harmo.hrp07 (keep = immig nimmig sond pnaic);
run;
proc freq data=rp;
table immig nimmig nimmig*pnaic;
run;
/* FIN */

%macro cg12(year);
/******************************************/
/* Macro introduce municipality code 2012 */
/******************************************/

/***************************************/
/* Ajuster pour PARIS LYON & MARSEILLE */
title "&year";

/* Premi�re �tape: remplace le CC du RP avec avec le CC2012 */
/* merge avec cg12 */
proc sort data=sample;
by depcom;
run;
data cg12;
set ccom.cg12;
run;
proc sort data=cg12 nodupkeys;
by depcom;
run;

/* correction Paris, Lyon, Marseille */
data sample;
merge sample (in = x) cg12;
by depcom;
if x;
/* correction car cc pour quartiers dans les RP68 & 75 pour Paris
et non cc commune */
if missing(cg12) & d="75" then cg12="75056";
run;

data sample;
set sample;
mcg12=missing(cg12);
run;
title "&year part de mauvaise fusion avec cc12 ";
proc freq data=sample;
table mcg12;
run;
%mend;
/* by Commuting zones */
/* average national immigrant share */
/* Merge with employment zones codes */
%macro ze;
/****************************/
proc sort data=sample;
by cgeo;
run;
proc sort data=result.ze_cgeo_reg;
by cgeo;
run;

data sample;
merge sample (in = x) result.ze_cgeo_reg (keep = cgeo ze dep reg);
by cgeo;
if x;
run;
%mend ze;
%macro inflows(year);
data sample;
set harmo.hrp&year (keep = cgeo sond immig nimmig pnaid pnaic) ;
depcom=cgeo;
d=substr(cgeo,1,2);
run;

/* national immigrant share */
title "national immigrant share &year";
proc freq data=sample;
weight sond;
table immig nimmig;
run;
/* serial correlation in national composition => recent arrival */
data immig;
set sample;
if immig=0 then delete;
run;
proc freq data=immig noprint;
weight sond;
table pnaid / out=pnaid&year;
run;
data pnaid&year;
set pnaid&year;
rename count=count&year;
rename percent=percent&year;
label count=count&year;
label percent=percent&year;
run;

/* share recent arrival from */
data nimmig;
set sample;
if nimmig=0 then delete;
run;
title "share recent arrivals from &year";
proc freq data=nimmig;
table pnaic;
run;
/* serial correlation in national composition => recent arrival */
proc freq data=nimmig noprint;
weight sond;
table pnaid / out=nipnaid&year;
run;
data nipnaid&year;
set nipnaid&year;
rename count=count&year;
rename percent=percent&year;
label count=count&year;
label percent=percent&year;
run;

/* share recent arrival by commuting zone */
%cg12(&year);
%ze;

/* Nation */
proc sort data=sample;
by ze;
run;
/* immigrant stock */
proc freq data=sample noprint;
by ze;
weight sond;
table immig  / out=ishare&year;
run;
data ishare&year;
set ishare&year;
if immig=0 then delete;
rename count= icount&year;
rename percent = ipercent&year;
label count= icount&year;
label percent = ipercent&year;
run;
proc freq data=sample noprint;
weight sond;
table ze / out=zepop&year;
run;
data zepop&year;
set zepop&year;
drop percent;
rename count = zepop;
label count = zepop;
run;
data ishare&year;
merge ishare&year zepop&year;
by ze;
drop immig;
if missing(ipercent&year) then ipercent&year.=0;
if missing(icount&year) then icount&year.=0;
run;
/* recent arrival */
proc freq data=sample noprint;
by ze;
weight sond;
table nimmig  / out=nishare&year;
run;
data nishare&year;
set nishare&year;
if nimmig=0 then delete;
rename count= nicount&year;
rename percent = nipercent&year;
label count= nicount&year;
label percent = nipercent&year;
run;
data nishare&year;
merge nishare&year zepop&year;
by ze;
drop nimmig;
if missing(nipercent&year) then nipercent&year.=0;
if missing(nicount&year) then nicount&year.=0;
run;

%mend;
%inflows(68);
%inflows(75);
%inflows(82);
%inflows(90);
%inflows(99);
%inflows(07);

/* serial correlation in national composition */
data pnaid;
merge pnaid68 pnaid75 pnaid82 pnaid90 pnaid99 pnaid07;
by pnaid;
run; 
data nipnaid;
merge nipnaid68 nipnaid75 nipnaid82 nipnaid90 nipnaid99 nipnaid07;
by pnaid;
run; 
title 'serial correlation immigrant stock';
proc corr data=pnaid;
var percent07 percent99 percent90 percent82 percent75 percent68;
run;
title 'serial correlation recent arrivals';
proc corr data=nipnaid;
var percent07 percent99 percent90 percent82 percent75 percent68;
run;
title 'average commuting zone 68';
proc means data=nishare68 n mean std cv;
var nipercent68;
run;
title 'average commuting zone 75';
proc means data=nishare75 n mean std cv;
var nipercent75;
run;
title 'average commuting zone 82';
proc means data=nishare82 n mean std cv;
var nipercent82;
run;
title 'average commuting zone 90';
proc means data=nishare90 n mean std cv;
var nipercent90;
run;
title 'average commuting zone 99';
proc means data=nishare99 n mean std cv;
var nipercent99;
run;
title 'average commuting zone 07';
proc means data=nishare07 n mean std cv;
var nipercent07;
run;

